package com.game.doudizhu.controller;

import com.game.doudizhu.model.Player;
import com.game.doudizhu.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/players")
public class PlayerController {

    @Autowired
    private PlayerService playerService;

    @PostMapping("/register")
    public Player register(@RequestParam("username") String username, @RequestParam("password")String password) {
        return playerService.register(username, password);
    }

    @PostMapping("/login")
    public Player login(@RequestParam("username") String username, @RequestParam("password")String password) {
        return playerService.login(username, password);
    }

    @GetMapping("/{id}")
    public Player getPlayer(@PathVariable Long id) {
        return playerService.getPlayer(id);
    }

    @GetMapping("/{id}/score")
    public Integer getPlayerScore(@PathVariable Long id) {
        Player player = playerService.getPlayer(id);
        return player.getScore();
    }

    @GetMapping("/{id}/stats")
    public PlayerStats getPlayerStats(@PathVariable Long id) {
        Player player = playerService.getPlayer(id);
        PlayerStats stats = new PlayerStats(
            player.getScore(),
            player.getGamesPlayed(),
            player.getGamesWon(),
            player.getLandlordGames(),
            player.getLandlordWins(),
            player.getFarmerGames(),
            player.getFarmerWins()
        );
        return stats;
    }

    // 用于返回玩家统计数据的内部类
    private static class PlayerStats {
        private final int score;
        private final int gamesPlayed;
        private final int gamesWon;
        private final int landlordGames;
        private final int landlordWins;
        private final int farmerGames;
        private final int farmerWins;

        public PlayerStats(int score, int gamesPlayed, int gamesWon, 
                         int landlordGames, int landlordWins, 
                         int farmerGames, int farmerWins) {
            this.score = score;
            this.gamesPlayed = gamesPlayed;
            this.gamesWon = gamesWon;
            this.landlordGames = landlordGames;
            this.landlordWins = landlordWins;
            this.farmerGames = farmerGames;
            this.farmerWins = farmerWins;
        }

        // Getters
        public int getScore() { return score; }
        public int getGamesPlayed() { return gamesPlayed; }
        public int getGamesWon() { return gamesWon; }
        public int getLandlordGames() { return landlordGames; }
        public int getLandlordWins() { return landlordWins; }
        public int getFarmerGames() { return farmerGames; }
        public int getFarmerWins() { return farmerWins; }
    }
} 